﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraBars;
using System.Reflection;
using DevExpress.XtraBars.Ribbon;

namespace td.Core.DiscoverAddins
{
    public partial class Reflection :  DevExpress.XtraEditors.XtraForm
    {
        public string Result = "";
        td.Core.Xml.AddIn addin;
        public Reflection(td.Core.Xml.AddIn addin)
        {
            InitializeComponent();
            this.addin = addin;
            for (int i = 0; i < addin.Runtimes.Count; i++)
            {
                this.listruntimes.Items.Add(this.addin.Runtimes[i].AssemblyName);
            }
            UpdateClassList(0);
            this.listclass.SelectedIndexChanged += new EventHandler(listclass_SelectedIndexChanged);
            this.listruntimes.SelectedIndexChanged += new EventHandler(listruntimes_SelectedIndexChanged);
            this.CenterToScreen();
        }

        private void UpdateClassList(int index)
        {
            this.listclass.Items.Clear();
            if (this.listruntimes.Items.Count > 0)
            {
                var list = LoadAddinNameFromDll(listruntimes.Items[index].ToString());
                for (int i = 0; i < list.Count; i++)
                {
                    this.listclass.Items.Add(list[i]);
                }
            }
        }

        void listruntimes_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.listclass.Items.Count > 0)
            {
                
                UpdateClassList(this.listruntimes.SelectedIndex);
            }
        }

        void listclass_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.listclass.SelectedItem != null)
            {
                this.Result = this.listclass.SelectedItem.ToString();
            }
        }

        public List<string> LoadAddinNameFromDll(string path)
        {
            List<string> list = new List<string>();
            Assembly assembly = Assembly.LoadFrom(Application.StartupPath+"\\"+path);
            Type[] types = assembly.GetTypes();
            foreach (var t in types)
            {
                try
                {
                    var obj = assembly.CreateInstance(t.ToString());
                    if (obj is td.Core.AbstractToolButtonCommand)
                    {
                        list.Add(t.ToString());
                    }
                    else if (obj is td.Core.AbstractViewContent)
                    {
                        list.Add(t.ToString());
                    }
                    else if (obj is td.Core.AbstractPad)
                    {
                        list.Add(t.ToString());
                    }
                }
                catch (Exception ee)
                {
                }
            }
            return list;
            
        }

    

        private void sbOK_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

    }
}